package com.chenyi.web.controller.system;

import java.util.List;

import com.chenyi.common.utils.AdminUtils;
import com.chenyi.common.utils.ShopUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import com.chenyi.common.annotation.Log;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.core.domain.entity.SysShop;
import com.chenyi.shop.service.ISysShopService;
import com.chenyi.common.utils.poi.ExcelUtil;
import com.chenyi.common.core.page.TableDataInfo;

/**
 * 商户管理Controller
 * 
 * @author chenyi
 * @date 2020-10-22
 */
@RestController
@RequestMapping("/admin/shop/shop")
public class AdminShopController extends BaseController
{
    @Autowired
    private ISysShopService sysShopService;

    /**
     * 查询商户管理列表
     */
    @PreAuthorize("@admin.hasPermi('shop:shop:list')")
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(SysShop sysShop)
    {
        startPage();
        List<SysShop> list = sysShopService.selectSysShopList(sysShop);
        return new ResponseEntity<>(getDataTable(list),HttpStatus.OK);
    }

    /**
     * 导出商户管理列表
     */
    @PreAuthorize("@admin.hasPermi('shop:shop:export')")
    @Log(title = "商户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<Object> export(SysShop sysShop)
    {
        List<SysShop> list = sysShopService.selectSysShopList(sysShop);
        ExcelUtil<SysShop> util = new ExcelUtil<SysShop>(SysShop.class);
        return util.exportExcel(list, "shop");
    }

    /**
     * 获取商户管理详细信息
     */
    @PreAuthorize("@admin.hasPermi('shop:shop:query')")
    @GetMapping(value = "/{shopId}")
    public ResponseEntity<Object> getInfo(@PathVariable("shopId") Long shopId)
    {
        return ResponseEntity.ok(sysShopService.selectSysShopById(shopId));
    }

    /**
     * 新增商户管理
     */
    @PreAuthorize("@admin.hasPermi('shop:shop:add')")
    @Log(title = "商户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Object> add(@RequestBody SysShop sysShop)
    {

        sysShop.setPassword(ShopUtils.encryptPassword(sysShop.getPassword()));
        sysShop.setCreateBy(AdminUtils.getUsername());
        sysShopService.insertSysShop(sysShop);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改商户管理
     */
    @PreAuthorize("@admin.hasPermi('shop:shop:edit')")
    @Log(title = "商户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Object> edit(@RequestBody SysShop sysShop)
    {
        sysShop.setUpdateBy(AdminUtils.getUsername());
        sysShopService.updateSysShop(sysShop);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除商户管理
     */
    @PreAuthorize("@admin.hasPermi('shop:shop:remove')")
    @Log(title = "商户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{shopIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] shopIds)
    {
        sysShopService.deleteSysShopByIds(shopIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
